/* WritePage.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   JOHN_BENNY	2008-7-10	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.download;

import java.io.File;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import framework.core.DBConnection;

import system.base.Warehouse;

/** 向页面打印*/
public class WritePage {
    /** 获得所需的集合     类型(_000000_材料分类文件)/产品(_000000_材料编码文件)*/
    public Vector getProductOrCatrgoryVec(Vector vec,String vecName){
        int size = vec.size();
        Vector needVec = new Vector();
        for (int i = 0; i < size; i++) {
            Map map = (Map)vec.get(i);
            String path = String.valueOf(map.get(FileBusiness.mapPath));
            int index = path.indexOf(vecName);
            if(index > 0)
                needVec.add(map);
        }
        return needVec;
    }
    
    /** 获得仓库更新集合*/
    public Vector getWarehouseVec(Vector vec){
        int size = vec.size();
        Vector needVec = new Vector();
        for (int i = 0; i < size; i++) {
            Map map = (Map)vec.get(i);
            String isFile = String.valueOf(map.get(FileBusiness.mapIsFile));
            if(isFile.equals("false")){
                needVec.add(map);
            }
        }
        return needVec;
    }
    
    /** 画表格*/
    public void draw(HttpServletRequest request, HttpServletResponse response,Vector drawVec,PrintWriter out) throws Exception{
        int size = drawVec.size();//集合大小
        int rowCount = size%3;//本集合将会产生的行数
        if(rowCount == 0){
            rowCount = size/3;
        }else{
            rowCount = size/3 + 1;
        }
        //循环行
        String fileName = null;
        String isFile = null;
        String filePath = null;
        String sub = null;
        for (int i = 0; i < rowCount; i++) {
            out.println("<tr>");
            //打印"<tr>"
            //循环列
            for (int j = i * 3; j < 3 * (i + 1); j++) {
                //1:循环的集合必须是单一的数据集对象(仓库/类别/产品)
                Map map = null;
                if(j >=  size){
                    out.println("<td width=\"260\"></td>");
                    //当发现元素提取超出索引
                    //则直接打印空的单元格
                }else{
                    map = (Map)drawVec.get(j);
                    fileName = (String)map.get(FileBusiness.mapFileName);
                    isFile = (String)map.get(FileBusiness.mapIsFile);
                    filePath = (String)map.get(FileBusiness.mapPath);
                    if(isFile.equals("true")){
                      out.println("<td width=\"260\"><input type=\"button\" value="+fileName+" onClick=\"getFile(this.value)\"/></td>");
                        //打印
                    }else{
                        //Vector subVec = (Vector)map.get(FileBusiness.mapSub);
                        //draw(request, response, subVec, out);
                        //应该不会存在此可能性:
                        //因在最初的集合提取时,对集合进行了分类.如果是文件夹,那么本方法本次执行全部是文件夹,即仓库信息.
                    }
                }
                //此段代码需要解决的逻辑问题
                //1:末尾行时判断列的提取不要紧越界     Done
                //2:循环进行更改,每次需提取三个元素(文件状态时)     Done
                //3:明确判断文件和文件件的不同,作出不同的动作   
                //4:仓库信息的特殊性,明确注意文件夹及文件的判断,不要产生行列错误问题.
                //5:请注意:一个vector是一个文件夹本曾的所有信息,而不包括上层或下层.若有下层,则在本元素的FileBusiness.mapSub中保存下层信息.
                
            }
            out.println("</tr>");
            //打印"</tr>"
        }
        out.flush();
    }
    
    
    /** 画表格*/
    public void draw(Vector drawVec,PrintWriter out,DBConnection con) throws Exception{
        int size = drawVec.size();//集合大小
        //循环行
        String fileName = null;
        String isFile = null;
        String filePath = null;
        String sub = null;
        Map map = null;
        for (int i = 0; i < size; i++) {
            map = (Map)drawVec.get(i);
            fileName = (String)map.get(FileBusiness.mapFileName);
            isFile = (String)map.get(FileBusiness.mapIsFile);
            filePath = (String)map.get(FileBusiness.mapPath);
            Vector subVec = (Vector)map.get(FileBusiness.mapSub);
            int subSize = subVec.size();
            //打印出仓库名称
            out.println("<tr>");
            out.println("<td width=\"260\" colspan=\"3\">"+Warehouse.getNameByCode(con, fileName)+"</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td colspan=\"3\"><hr width=\"750\"></td>");
            out.println("</tr>");
            int rowCount = subSize%3;//本集合将会产生的行数()
            if(rowCount == 0){
                rowCount = subSize/3;
            }else{
                rowCount = subSize/3 + 1;
            }
            //循环列
            //out.println("<tr>");
            for (int k = 0; k < rowCount; k++) {
                out.println("<tr>");
                for (int j = k * 3; j < 3 * (k + 1); j++) {
                    //1:循环的集合必须是单一的数据集对象(仓库/类别/产品)
                    if(j >=  subSize){
                        out.println("<td width=\"260\"></td>");
                        //当发现元素提取超出索引
                        //则直接打印空的单元格
                    }else{
                        map = (Map)subVec.get(j);
                        fileName = (String)map.get(FileBusiness.mapFileName);
                        isFile = (String)map.get(FileBusiness.mapIsFile);
                        filePath = (String)map.get(FileBusiness.mapPath);
                        if(isFile.equals("true")){
                          out.println("<td width=\"260\"><input type=\"button\" value="+fileName+" onClick=\"getFile(this.value)\"/></td>");
                            //打印
                        }else{
                            //draw(request, response, subVec, out);
                            //应该不会存在此可能性:
                            //因在最初的集合提取时,对集合进行了分类.如果是文件夹,那么本方法本次执行全部是文件夹,即仓库信息.
                        }
                    }
                    //此段代码需要解决的逻辑问题
                    //1:末尾行时判断列的提取不要紧越界     Done
                    //2:循环进行更改,每次需提取三个元素(文件状态时)     Done
                    //3:明确判断文件和文件件的不同,作出不同的动作   
                    //4:仓库信息的特殊性,明确注意文件夹及文件的判断,不要产生行列错误问题.
                    //5:请注意:一个vector是一个文件夹本曾的所有信息,而不包括上层或下层.若有下层,则在本元素的FileBusiness.mapSub中保存下层信息.
                }
                out.println("</tr>");
            }
            //out.println("<tr>");
            //打印"</tr>"
        }
        out.flush();
    }
}
